package com.example.demo.utils;


import com.example.demo.utils.exception.TException;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 检查参数工具类
 * 减少if使用次数
 */
public class AssertUtils {

    public static void notNull(Object value,String message) {
        if(value == null) {
            throw new TException(message);
        }
    }

    public static void isNull(Object value,String message) {
        if(value != null) {
            throw new TException(message);
        }
    }

    public static void notBlank(String value,String message) {
        if(StringUtils.isBlank(value)) {
            throw new TException(message);
        }
    }

    public static void isBlank(String value,String message) {
        if(StringUtils.isNotBlank(value)) {
            throw new TException(message);
        }
    }

    public static void notEmpty(List list, String message) {
        if(list == null||list.isEmpty()) {
            throw new TException(message);
        }
    }

    public static void isEmpty(List list, String message) {
        if(list != null&&!list.isEmpty()) {
            throw new TException(message);
        }
    }

    public static void requireTrue(boolean value, String msg) {
        if (!value) {
            throw new TException(msg);
        }
    }

    public static void requireFalse(boolean value, String msg) {
        if (value) {
            throw new TException(msg);
        }
    }

    public static void requireEquals(Object value1, Object value2, String msg) {
        if (!Objects.equals(value1, value2)) {
            throw new TException(msg);
        }
    }
}
